package com.hanfengyi.hrm;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * User: Han
 * Date: 2020/3/4
 * Time: 13:44
 * Description:
 */
public class NativeHttpUtils {
    /**
     * get方式发送请求
     * @param URL 目标地址
     * @param param 参数
     * @return
     */
    public static String doGet(String URL, Map<String,String>param){
        HttpURLConnection conn = null;
        InputStream is = null;
        BufferedReader br = null;
        StringBuilder result = new StringBuilder();
        try{
            //拼接url
            URL = URL.endsWith("/")?URL.substring(0,URL.lastIndexOf("/"))+"?":URL+"?";
            for (Map.Entry<String, String> keyAndValue : param.entrySet()) {
                URL = URL+keyAndValue+"&";
            }
            URL = URL.endsWith("&")?URL.substring(0,URL.lastIndexOf("&")):URL;
            System.out.println(URL);
            //创建远程url连接对象
            URL url = new URL(URL);
            //通过远程url连接对象打开一个连接，强转成HTTPURLConnection类
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            //设置连接超时时间和读取超时时间
            conn.setConnectTimeout(15000);
            conn.setReadTimeout(60000);
            conn.setRequestProperty("Accept", "application/json");
            //发送请求
            conn.connect();
            //通过conn取得输入流，并使用Reader读取
            if (200 == conn.getResponseCode()){
                is = conn.getInputStream();
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                String line;
                while ((line = br.readLine()) != null){
                    result.append(line);
                    System.out.println(line);
                }
            }else{
                System.out.println("ResponseCode is an error code:" + conn.getResponseCode());
            }
        }catch (MalformedURLException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(br != null){
                    br.close();
                }
                if(is != null){
                    is.close();
                }
            }catch (IOException ioe){
                ioe.printStackTrace();
            }
            conn.disconnect();
        }
        return result.toString();
    }

    public static void main(String[] args) {
        Map<String,String> param = new HashMap<>();
        param.put("name", "zs");
        param.put("age", "18");
        String URL = "https://way.jd.com/chuangxin/VerCodesms";
        URL = URL.endsWith("/")?URL.substring(0,URL.lastIndexOf("/"))+"?":URL+"?";
        for (Map.Entry<String, String> keyAndValue : param.entrySet()) {
            URL = URL+keyAndValue+"&";
        }
        URL = URL.endsWith("&")?URL.substring(0,URL.lastIndexOf("&")):URL;
        System.out.println(URL);

    }

}